package com.zzh.ut;
/*
 * @Auther : wenrouyue
 * @Date : 2022/3/6-23:18
 */

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyUtils {

    public static String getMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }

    public static String getCode() {
        int a = 1234567890;
        String b = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String c = a + b;
        char[] d = c.toCharArray();
        Random random = new Random();
        String f = "";
        for (int k = 0; k < 6; k++) {
            int index = random.nextInt(d.length);
            f += d[index];
        }
        return f;
    }

    public static Long getDateMillisecond(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            long beginMillisecond = format.parse(format.format(new Date())).getTime();
            long endMillisecond = format.parse(format.format(date)).getTime();
            return beginMillisecond - endMillisecond;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean getFailure(Long time){
        int failure = (int) (time / 1000);
        return failure <= 300 ? true : false;
    }
    public static Map<String,Object> getInfo(String info){
        HashMap<String, Object> map = new HashMap<>();
        map.put("info",info);
        return map;
    }
    public static boolean isEmail(String email){
        if (null==email || "".equals(email)){
            return false;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        boolean b = m.matches();
        return b ? true:false;
    }
}
